﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  UnitAdd
 作    者:曹俊、高文华
 编写日期:2009-07-04
 审    核:
 审核日期:
 描    述:部门新增
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;

using Cisoft.IntegrationSystem.Model;
using Cisoft.IntegrationSystem.BLL;

public partial class User_UnitAdd : WebUI.Page.BasePage
{
    private DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ChuShiHuaData();
            txtOrder.Text = "0";
        }
    }

    #region 初始化数据默认值绑定
    protected void ChuShiHuaData()
    {
        Initializtion(this.ddlParentUnit, 0, 0, "皇氏乳业");
    }
    #endregion

    #region 下拉框显示树型

    /// <summary>
    /// 初始化组合框级别（无限级栏目）
    /// </summary>
    /// <param name="dropList">组合框名称</param>
    /// <param name="iDepth">深度</param>
    /// <param name="iParentID">父类ID</param>
    public void Initializtion(DropDownList dropList, int iDepth, int iParentID, string strTip)
    {
        dropList.Items.Clear();
        dropList.Items.Add(new ListItem(strTip, "0"));

        ShowColumns(dropList, iDepth, iParentID);
    }

    /// <summary>
    /// 组合框显示所有级别（无限级级别）
    /// </summary>
    /// <param name="dropList">组合框名称</param>
    /// <param name="iDepth">深度</param>
    ///  <param name="iParentID">父类ID</param>int iParentID
    public void ShowColumns(DropDownList dropList, int iDepth, int iParentID)
    {



        //Cisoft.IntegrationSystem.BLL.MemberGroupBO bllgroup = new Cisoft.IntegrationSystem.BLL.MemberGroupBO();
        //Cisoft.IntegrationSystem.Model.MemberGroupMO modelgroup = new Cisoft.IntegrationSystem.Model.MemberGroupMO();
        UserGroupBO bllgroup = new UserGroupBO();
        UserGroupMO modelgroup = new UserGroupMO();
        //Power myPower = new Power();

        //定义变量
        int iCount = 0;
        int iSum = 0;
        string strGroupName = "";
        string strGroupID = "";


        DataSet myDataSet = new DataSet();

        myDataSet = bllgroup.GetListNewByRecordSort(" UG_GroupParentID =" + iParentID + "");
        iCount = myDataSet.Tables[0].Rows.Count;

        if (iCount != 0)
        {
            //iSum深度变理
            iSum = iDepth + 1;

            for (int i = 0; i < iCount; i++)
            {
                if (i == (iCount - 1))
                {
                    strGroupName = replaceWord(GetDepth("├◎" + myDataSet.Tables[0].Rows[i]["UG_GroupName"].ToString(), iSum));
                    strGroupID = myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString();

                    dropList.Items.Add(new ListItem(strGroupName, strGroupID));

                }
                else
                {
                    strGroupName = replaceWord(GetDepth("├◎" + myDataSet.Tables[0].Rows[i]["UG_GroupName"].ToString(), iSum));
                    strGroupID = myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString();

                    dropList.Items.Add(new ListItem(strGroupName, myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString()));
                }
                ShowColumns(dropList, iSum, Int32.Parse(myDataSet.Tables[0].Rows[i]["UG_GroupID"].ToString()));
            }
        }
    }

    /// <summary>
    /// 字符串插入"│"字符
    /// </summary>
    /// <param name="str">需要插入"│"的字符串</param>
    /// <param name="i">插入"│"字符串的索引位置</param>
    /// <returns></returns>
    private static string GetDepth(string strGroupName, int iCount)
    {
        for (int i = 1; i < iCount; i++)
        {
            strGroupName = strGroupName.Insert(0, "│");
        }

        return strGroupName;
    }

    /// <summary>
    /// 将指定字符串里的“&nbsp;”替换为空字符
    /// </summary>
    /// <param name="strText"></param>
    /// <returns></returns>
    private string replaceWord(string strText)
    {
        string outText = strText.Replace("&nbsp;", " ");
        return outText;
    }

    #endregion

    #region 新增事件
    protected void submitBtn_Click(object sender, EventArgs e)
    {
        string errortip = "";

        UserGroupBO ugBO = new UserGroupBO();
        UserGroupMO ugMO = new UserGroupMO();

        ugMO.UserGroupID = ugBO.GetMaxID();
        ugMO.UserGroupName = txtName.Text;
        ugMO.UserGroupOrder = int.Parse(txtOrder.Text.Trim());
        ugMO.UserGroupParentID = int.Parse(ddlParentUnit.SelectedValue);
        ugMO.UserGroupRole = 0;

        try
        {
            ugBO.AddRecord(ugMO);
        }
        catch(Exception ex)
        {
            errortip = ex.Message;
        }
        if (errortip != "")
        {
            PageError(errortip, "UnitManage.aspx");
        }
        else
        {
            PageRight("新增成功", "UnitManage.aspx", "返回管理", "");
        }

    }
    #endregion

    #region 返回事件
    protected void ibtnback_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("UnitManage.aspx");
    }
    #endregion

}
